/*-------------------------------------------------------------------------------
* file:     app_uart.h
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   
*-------------------------------------------------------------------------------*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __APP_UART_H__
#define __APP_UART_H__

/* Includes ------------------------------------------------------------------*/
#include "common.h"
#include "pin.h"
#include "uart.h"
/*----------------------------------------------------------------------------*/
#include "app_init.h"
#include "app_cfg.h"
#include "app_timer.h"
#include "one_wire_bus_app.h"
/*----------------------------------------------------------------------------*/
extern volatile uint8_t release_tx_flag;

extern bool uart_4ms_timer_flag;                  /* 4ms时基标志位 */
extern uint8_t uart_4ms_timer_tick;               /* 时基计时tick */
extern bool uart_500ms_timer_flag;                  /* 500ms时基标志位 */
extern uint32_t uart_500ms_timer_tick;               /* 时基计时tick */

extern bool f_uart_rec_one_frame;
/* Private macro-----------------------------------------------------------*/
/*          
 *  1. 每一个“单总线数据包”最大99 Byte
 *  2. 级连后的最大“串口数据帧”长度200Byte
 */
#define APP_UART_TX_PHY_BUF_SIZE            (ONE_WIRE_PHY_FRAME_SEND_MAX_LENGTH)
#define APP_UART_RX_PHY_BUF_SIZE            (ONE_WIRE_PHY_FRAME_REC_MAX_LENGTH)
#define APP_UART_RX_HEAD_BACK_SIZE          8
#define APP_UART_ONE_WIRE_MIN_LEN           12
#define UART_ONE_WIRE_BAUDRATE              460800
#define UART_BASE                           UART2
#define UART_IRQ_NUM                        UART2_IRQ_NUM
#define UART_IDX                            2
#define UART_TX                             PB00
#define UART_RX                             PB01           
#define UART_TX_UART                        PB00_UART2_TX
#define UART_RX_UART                        PB01_UART2_RX
#define UART_TX_GPD                         PB00_GPD            // Tx 高阻态
#define UART_RX_INPUT                       PB01_INPUT          // Rx 输入
#define UART_SCAN_TIMER_N                   5                  // *100US
/*----------------------------------------------------------------------------*/
/**
 * @brief : 串口上电初始化函数
 */
void app_uart_init(void);

/**
 * @brief 用于uart trx引脚配置
 * 
 */
void app_uart_init_io_set(void);

/**
 * @brief : 串口接收超时处理函数
 * 
 * @param byIdx: 串口号
 * @param rx_len: 接收到数据长度
 * @return 
 */
void app_uart_rx_timerout_callback(uint8_t byIdx,uint16_t rx_len);

/**
 * @brief 单总线计时时基
 * 
 */
void app_uart_timer_tick_0_1ms_handler(void);

/**
 * @brief : 向串口发送一帧数据
 * 
 * @param pbuf 数据缓存指针
 * @param len  数据长度
 * @return true 发送成功
 * @return false 发送失败
 */
extern bool one_wire_bus_uart_send_one_packet(uint8_t * pbuf,uint16_t len);

/**
 * @brief : 串口发送完处理函数
 * 
 * @param byIdx: 串口号
 * @return 
 */
void app_uart_tx_done_callback(uint8_t byIdx);

/**
 * @brief : 串口处理进程
 * 
 */
void app_uart_process(void);

/**
 * @brief 用于uart trx引脚配置
 * 
 */
void app_uart_init_io_set(void);

/**
 * @brief 休眠唤醒切换
 * 
 */
void app_uart_sleep_and_weakup_io_set(bool on);

void app_uart_doze_and_weakup_io_set(bool on);

#endif

